package cl.rizepe.playntest.core.effects;

import pythagoras.f.Point;
import cl.rizepe.playntest.core.menu.Menu;
import cl.rizepe.playntest.core.menu.MenuDirection9;
import cl.rizepe.playntest.core.menu.MenuOriginGrid9;
import cl.rizepe.playntest.core.views.EffectedLayer;

public class EffectMenu implements Effect, OnLayerEffect {
	Menu menu;
	boolean drawn=false;
	EffectedLayer layerState;
	public EffectedLayer getLayerState() {
		return layerState;
	}

	public void setLayerState(EffectedLayer layerState) {
		this.layerState = layerState;
	}

	int location,direction;
	
	public EffectMenu(EffectedLayer layerState, int location, int direction){
		this.direction=direction;
		this.location=location;
		this.layerState=layerState;
    	layerState.addEffect(this);
    	initMenu(location, direction);
	}

	private void initMenu(int location, int direction) {
		//ImageLayer imageLayer=this.layerState.getImageLayer();
    	//Point size =new Point (imageLayer.width(),imageLayer.height());
    	//Transform transform =  imageLayer.transform();
    	
    	//Point center = new Point(transform.tx(),transform.ty());
    	Point center =layerState.getTranslate();
    	Point size = layerState.getSize();
    	MenuOriginGrid9 mo9 = new MenuOriginGrid9(center, size, location);
    	MenuDirection9 md9=new MenuDirection9(mo9.getMenuOrigin(),size,direction);
    	
    	menu = new Menu ( mo9, md9,size);
	}
	
	public void updateMenu(){
//		ImageLayer imageLayer=this.layerState.getImageLayer();
  //  	Point size =new Point (imageLayer.width(),imageLayer.height());
		Point size =layerState.getSize();
    	//Transform transform =  imageLayer.transform();
    	//Point center = new Point(transform.tx(),transform.ty());
		Point center = layerState.getTranslate();
		menu.getOrigin().setCenter(center);
		menu.getOrigin().setButtonSize(size);
		menu.getOrigin().setLocation(location);
		menu.getDirection().setOrigin(menu.getOrigin().getMenuOrigin());
		menu.getDirection().setButtonSize(size);
		menu.getDirection().setDirection(direction)	;
	}
	
	@Override
	public void toggle() {
		if (drawn)
			erase();
		else
			apply();
		
	}

	public Menu getMenu(){
		return menu;
	}
	@Override
	public void refresh() {	
		if (drawn)
			apply();
	}

	@Override
	public void apply() {
		updateMenu();		
		menu.draw();		
		drawn=true;
	}

	@Override
	public void erase() {
		menu.erase();
		drawn=false;
	}

}
